<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    console.log(0.1 + 0.2)//0.30000000000000004
    //为解决这个问题，自己封装一个函数
    const add = (x, y) => {
      try {
        const magnificationlength = Math.max(x.toString().split('.')[1].length, y.toString().split('.')[1].length)
        const magnification = Math.pow(10, magnificationlength)
        return ((x * magnification) + (y * magnification)) / magnification
      } catch (errot) {
        return x + y
      }
    }
    console.log(add(1, 0.2))
  </script>
</body>

</html>